package net.jxvtc.blogv1.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.sql.DataSource;

/**
 * @program: blog_v1
 * @description:
 * @author: 蓝建平
 * @create: 2020-10-21 21:36
 **/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/css/**", "/font/**",
                        "/js/**", "/index")
                .permitAll()
                .antMatchers("/users", "/admins", "/comments")
                .hasRole("admin")
                .antMatchers("/h2-console/**").permitAll()
                .and()
                .formLogin().loginPage("/login")
                .failureUrl("/login");

        http.logout().logoutUrl("/myLogout")
                .logoutSuccessUrl("/index");

        http.csrf().ignoringAntMatchers("/h2-console/**");

        http.headers().frameOptions().sameOrigin();


    }

//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        auth.inMemoryAuthentication().passwordEncoder(encoder)
//                .withUser("user1").password(encoder.encode("123456"))
//                .roles("common")
//                .and()
//                .withUser("admin").password(encoder.encode("123456"))
//                .roles("admin");
//
//
//    }

//    @Autowired
//    private DataSource dataSource;
//
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        String sql1="select username,password,valid from t_customer where username=?";
//        String sql2="select c.username,a.authority from t_customer c,"
//                +"t_authority a,t_customer_authority ca "
//                +" where ca.customer_id=c.id and ca.authority_id=a.id and c.username=?";
//        auth.jdbcAuthentication().passwordEncoder(encoder)
//                .dataSource(dataSource)
//                .usersByUsernameQuery(sql1)
//                .authoritiesByUsernameQuery(sql2);
//    }

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(); //使用BCrypt加密
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        auth.userDetailsService(userDetailsService).passwordEncoder(encoder);
    }

}
